//
//  GradientButton.swift
//  SCA
//
//  Created by tjun on 2025/9/1.
//

import UIKit

//class GradientButton: UIButton {
//    
//    var colors: [UIColor]? {
//        didSet { updateColors() }
//    }
//    
//    var locations: [NSNumber]? {
//        didSet { gradientLayer.locations = locations }
//    }
//    
//    var isGradientBorder: Bool = false {
//        didSet { updateBorderMask() }
//    }
//    
//    var borderWidth: CGFloat = 2.0 {
//        didSet { updateBorderMask() }
//    }
//    
//    var cornerRadius: CGFloat = 8.0 {
//        didSet { updateBorderMask() }
//    }
//    
//    override var isEnabled: Bool {
//        didSet {
//            updateAppearance()
//        }
//    }
//    
//    override class var layerClass: AnyClass { CAGradientLayer.self }
//    private var gradientLayer: CAGradientLayer { layer as! CAGradientLayer }
//    private let borderMask = CAShapeLayer()
//    
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        setupDefault()
//    }
//    
//    required init?(coder: NSCoder) {
//        super.init(coder: coder)
//        setupDefault()
//    }
//    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        updateBorderMask()
//    }
//    
//    private func setupDefault() {
//        colors = [UIColor.white.withAlphaComponent(0), .red]
//        setStartPoint(CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1))
//        updateColors()
//    }
//    
//    private func updateColors() {
//        gradientLayer.colors = colors?.map { $0.cgColor }
//    }
//    
//    private func updateBorderMask() {
//        if isGradientBorder {
//            let path = UIBezierPath(roundedRect: bounds.insetBy(dx: borderWidth / 2, dy: borderWidth / 2),
//                                    cornerRadius: cornerRadius)
//            borderMask.path = path.cgPath
//            borderMask.fillColor = UIColor.clear.cgColor
//            borderMask.lineWidth = borderWidth
//            borderMask.strokeColor = UIColor.black.cgColor
//            gradientLayer.mask = borderMask
//            backgroundColor = .clear
//        } else {
//            gradientLayer.mask = nil
//        }
//    }
//    
//    func setStartPoint(_ startPoint: CGPoint, endPoint: CGPoint) {
//        gradientLayer.startPoint = startPoint
//        gradientLayer.endPoint = endPoint
//    }
//    
//    private func updateAppearance() {
//        if isEnabled {
//            alpha = 1.0
//        } else {
//            alpha = 0.5
//        }
//    }
//}
//
//


class GradientButton: UIButton {
    
    var colors: [UIColor]? {
        didSet { updateColors() }
    }
    var locations: [NSNumber]? {
        didSet { gradientLayer.locations = locations }
    }
    var isGradientBorder: Bool = false {
        didSet { updateBorderMask() }
    }
    var borderWidth: CGFloat = 2.0 {
        didSet { updateBorderMask() }
    }
    var cornerRadius: CGFloat = 8.0 {
        didSet { updateBorderMask() }
    }
    
    override var isEnabled: Bool {
        didSet {
            updateAppearance()
        }
    }
    
    private let gradientLayer = CAGradientLayer()
    private let borderMask = CAShapeLayer()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupDefault()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupDefault()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = bounds
        updateBorderMask()
    }
    
    private func setupDefault() {
        backgroundColor = .clear
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 0, y: 1)
        layer.addSublayer(gradientLayer)
        updateColors()
    }
    
    private func updateColors() {
        gradientLayer.colors = colors?.map { $0.cgColor }
    }
    
    private func updateBorderMask() {
        guard isGradientBorder else {
            gradientLayer.mask = nil
            return
        }
        let path = UIBezierPath(
            roundedRect: bounds.insetBy(dx: borderWidth / 2, dy: borderWidth / 2),
            cornerRadius: cornerRadius
        )
        borderMask.path = path.cgPath
        borderMask.fillColor = UIColor.clear.cgColor
        borderMask.strokeColor = UIColor.black.cgColor
        borderMask.lineWidth = borderWidth
        gradientLayer.mask = borderMask
    }
    
    func setStartPoint(_ startPoint: CGPoint, endPoint: CGPoint) {
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
    }
    
    private func updateAppearance() {
        if isEnabled {
            alpha = 1.0
        } else {
            alpha = 0.3
        }
    }
}
